package Day2025_04_01;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class LeetCode230 {
    int cur;
    int ret;
    public int kthSmallest(TreeNode root, int k) {
        cur = k;
        dfs(root);
        return ret;
    }
    private void dfs (TreeNode root) {
        if (root == null || cur == 0) return;
        dfs(root.left);
        if (cur == 0) return;
        cur--;
        ret = root.val;
        dfs(root.right);
    }

    public static void main(String[] args) {
        LeetCode230 leetCode230 = new LeetCode230();
        System.out.println(leetCode230.kthSmallest(new TreeNode(3, new TreeNode(1, null, new TreeNode(2)), new TreeNode(4)), 1));
    }
}
